package com.victor.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.address.Address;
import com.victor.entity.user.User;
import com.victor.entity.user.UserBankcard;
import com.victor.service.AddressService;
import com.victor.service.My_bankcardService;
import com.victor.service.MyselfService;
import com.victor.service.UserService;
import com.victor.utils.CodeUtil;
import com.victor.utils.Msg;
import com.victor.utils.sms.SmsSend;

/**
 * 我的 --- 必备
 * 
 * @author zhangbin
 *
 */
@RestController
@RequestMapping("/myself")
public class Myself_bb_C {

	@Autowired
	private UserService userService;

	@Autowired
	private My_bankcardService bankcardService;

	@Autowired
	private MyselfService myselfService;

	@Autowired
	private AddressService addressService;

	/**
	 * 登录 密码找回 密码更改 验证码有效期10分钟 银行卡验证 实名认证 验证码有效期5分钟
	 */
	// 定义定时器
	private Timer timer = new Timer();

	// 定义验证码list
	private static List<Map<String, Object>> codelist = new ArrayList<Map<String, Object>>();

	// app手机号验证码登录 --- 登录验证码 ----- 5分钟验证码 传34
	@RequestMapping("/buildcode_5")
	public Msg buildcode_5(String mobile, int type) {
		// 3.银行卡验证(11304)
		// 4 实名认证(11305)

		if (mobile == null || mobile.equals("")) {
			return Msg.fail("手机号为空");
		}

		// 关闭timer
		timer.cancel();
		// new一个Timer
		timer = new Timer();

		Map<String, Object> m2 = new HashMap<String, Object>();
		// 生成验证码(6位随机的验证码)
		String random = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
		System.out.println(mobile + " ~~~" + random);
		// 第三方发送短息
		int result = 0;
		if (type == 0) {
			result = SmsSend.smsSendMessage(mobile, "11301", random);// 0.登录(11301)
		} else if (type == 1) {
			result = SmsSend.smsSendMessage(mobile, "11302", random); // 1.密码找回(11302)
		} else if (type == 2) {
			result = SmsSend.smsSendMessage(mobile, "11303", random);// 2. 密码更改(11303)
		} else if (type == 3) {
			result = SmsSend.smsSendMessage(mobile, "11304", random);// 3.银行卡验证(11304)
		} else if (type == 4) {
			result = SmsSend.smsSendMessage(mobile, "11305", random);// 4 实名认证(11305)
		} else {
			return Msg.fail("type错误");
		}

		if (result == 1) {
			// 删除该手机号在codelist中其他的验证信息
			CodeUtil.clearothermap(codelist, mobile);
			// 将随机验证码放入到全局map中
			m2.put("random", random);
			m2.put("mobile", mobile);
			codelist.add(m2);
			m2 = new HashMap<String, Object>();

			// 设置定时器2分钟清空mmmlist
			timer.schedule(new TimerTask() {
				public void run() {
					codelist.clear();
				}
			}, 5 * 60 * 1000);// 设定指定的时间time,此处为5*60*1000毫秒

			return Msg.success().add("code", random);
		} else {
			return Msg.fail("验证码三方发送失败");
		}
	}

	/** 实名认证 */
	// 通过userid查询该用户是否已经实名认证
	@RequestMapping("/ifCertification")
	public Msg ifCertification(Integer userid) {
		Integer certification = myselfService.ifCertification(userid);
		return Msg.success().add("certification", certification);// certification为0时 未实名认证 为1时 已实名认证
	}

	// 提交实名认证信息
	@RequestMapping("/submitCertification")
	public Msg getCertification(@RequestBody User user) {
		if (user == null) {
			return Msg.fail("参数错误");
		}
		List<UserBankcard> userBankcards = user.getUserBankcards();
		if (userBankcards == null || userBankcards.size() <= 0) {
			return Msg.fail("银行卡参数错误");
		}
		// 加入实名认证标志
		user.setCertification("1");
		// 拿出银行卡信息
		// 进行银行卡四维验证 TODO
		// 修改user
		boolean updResult = userService.updateUser(user);
		if (updResult) {
			for (UserBankcard userBankcard : userBankcards) {
				userBankcard.setUserid(String.valueOf(user.getId()));
				// 添加银行卡信息
				userService.insUserBankCard(userBankcard);
			}
			return Msg.success();
		}
		return Msg.fail();
	}

	/** 银行卡 */
	// 获取我的银行卡
	@RequestMapping("/getUserBankcards")
	public Msg getUserBankcards(String userid) {
		List<UserBankcard> userBankcards = bankcardService.selBankcardListByUserid(userid);

		return Msg.success().add("userBankcards", userBankcards);
	}

	// 添加我的银行卡
	@RequestMapping("/addUserBankcard")
	public Msg addUserBankcard(UserBankcard userBankcard) {
		// 银行卡四要素认证 TODO
		boolean bindBrandCard = bankcardService.bindBrandCard(userBankcard);
		return Msg.judge(bindBrandCard);
	}

	// 删除银行卡
	@RequestMapping("/delUserBankcard")
	public Msg delUserBankcard(Integer bankcardid) {
		boolean unbindBrandCard = bankcardService.unbindBrandCard(bankcardid);
		return Msg.judge(unbindBrandCard);
	}

	/** 购票信息 TODO */

	/** 我的地址 */
	// 添加地址
	@RequestMapping("/addAddress")
	public Msg addAddress(Address address) {
		if (address == null || address.getUid() == null || address.getUid().isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = addressService.insert(address);
		if (!b) {
			return Msg.fail("添加地址错误");
		}
		return Msg.success();

	}

	/**
	 * 修改地址
	 * 
	 * @param address
	 * @return
	 */
	@RequestMapping("/updateaddress")
	public Msg updateAddress(Address address) {
		if (address == null || address.getId() == null) {
			return Msg.fail("入参失败");
		}
		boolean b = addressService.update(address);
		if (!b) {
			return Msg.fail("修改失败");
		}
		return Msg.success();
	}

	/**
	 * 地址详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/findXiangqingAddress")
	public Msg findXiangqingAddress(Integer addid) {
		if (addid == null) {
			return Msg.fail("入参失败");
		}
		Address address = addressService.selById(addid);
		return Msg.success().add("address", address);
	}

	/**
	 * 查询全部地址
	 * 
	 * @param page
	 * @param row
	 * @param uid
	 * @return
	 */
	@RequestMapping("/findAllAddress")
	public Msg findAllAddress(int page, int row, String uid) {
		if (uid == null || uid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<Address> all = addressService.selAll(page, row, uid);
		return Msg.success().add("all", all);
	}

	/**
	 * 设为 默认地址
	 * 
	 * @param id
	 * @param uid
	 * @return
	 */
	@RequestMapping("/morenAddress")
	public Msg morenAddress(Integer id, String uid) {
		if (id == null || uid == null || uid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean b = addressService.updMoren(id, uid);
		if (!b) {
			return Msg.fail("修改默认地址失败");
		}
		return Msg.success();
	}

	/**
	 * 删除地址
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteAddress")
	public Msg deleteAddress(int id) {
		if (id == 0) {
			return Msg.fail("入参失败");
		}
		boolean b = addressService.delete(id);
		if (!b) {
			return Msg.fail("删除失败");
		}
		return Msg.success();
	}

	/**
	 * 查询默认地址
	 * 
	 * @param uid
	 * @return
	 */
	@RequestMapping("/getMorenAddress")
	public Msg getMorenAddress(String uid) {
		if (uid == null || uid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		Address moren = addressService.selMoren(uid);
		return Msg.success().add("address", moren);
	}

}
